import logging, formbuild, datetime
from pylons.decorators import rest
from eshop.lib.base import *
from authkit.authorize.pylons_adaptors import authorize
from authkit.permissions import RemoteUser, ValidAuthKitUser

log = logging.getLogger(__name__)

class CommentController(BaseController):

    @rest.dispatch_on(POST='save_add')
    def add(self, id):
        c.comment = model.Comment.q.get(id)
        c.form = model.forms.build.StandardForm(dict(request.params))
        return render('/catalog/comment_add.mako')
    
       
    def save_add(self, id):
        c.captcha = session.get('ESHOP_CAPTHCHA')     
        c.comment = model.Comment.q.get(id)  
        results, errors, response = formbuild.handle(  
            schema=model.forms.schema.AddCommentFormSchema(),  
            template='/catalog/comment_add.mako',  
            form=model.forms.build.StandardForm  
        )  
        if response:
            return response   
        comment = model.Comment(id=None)
        comment.text = results['text'];
        comment.author_name = results['author_name']
        comment.author_ip = request.environ.get('REMOTE_ADDR')
        comment.created = datetime.datetime.now();
        c.comment.child_comments.append(comment); 
        comment.parent_comment = c.comment
           
        redirect_to(controller='product')
 
